package com.yida.gwj.finance.service.impl;

import com.yida.common.core.enums.UserStatus;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.gwj.api.domain.GwjFinanceBank;
import com.yida.gwj.finance.mapper.GwjFinanceBankMapper;
import com.yida.gwj.finance.service.IGwjFinanceBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 银行信息Service业务层处理
 *
 * @author lqq
 * @date 2022-12-30
 */
@Service
public class GwjFinanceBankServiceImpl implements IGwjFinanceBankService {
	@Resource
	private GwjFinanceBankMapper financeBankMapper;

	/**
	 * 查询银行信息
	 *
	 * @param id 银行信息主键
	 * @return 银行信息
	 */
	@Override
	public GwjFinanceBank selectFinanceBankById(Long id) {
		return financeBankMapper.selectFinanceBankById(id);
	}

	/**
	 * 查询银行信息列表
	 *
	 * @param financeBank 银行信息
	 * @return 银行信息
	 */
	@Override
	public List<GwjFinanceBank> selectFinanceBankList(GwjFinanceBank financeBank) {
		return financeBankMapper.selectFinanceBankList(financeBank);
	}

	/**
	 * 新增银行信息
	 *
	 * @param financeBank 银行信息
	 * @return 结果
	 */
	@Override
	public int insertFinanceBank(GwjFinanceBank financeBank) {
		GwjFinanceBank temp = new GwjFinanceBank();
		temp.setBankId(financeBank.getBankId());
		temp.setDelFlag(UserStatus.OK.getCode());
		temp.setAreaId(financeBank.getAreaId());
		if (StringUtils.isNotNull(financeBankMapper.selectFinanceBank(temp))) {
			throw new ServiceException("银行已存在");
		}
		return financeBankMapper.insertFinanceBank(financeBank);
	}

	/**
	 * 修改银行信息
	 *
	 * @param financeBank 银行信息
	 * @return 结果
	 */
	@Override
	public int updateFinanceBank(GwjFinanceBank financeBank) {
		GwjFinanceBank temp = new GwjFinanceBank();
		temp.setBankId(financeBank.getBankId());
		temp.setDelFlag(UserStatus.OK.getCode());
		temp.setAreaId(financeBank.getAreaId());
		temp = financeBankMapper.selectFinanceBank(temp);
		if (StringUtils.isNotNull(temp) && !financeBank.getId().equals(temp.getId())) {
			throw new ServiceException("银行已存在");
		}
		return financeBankMapper.updateFinanceBank(financeBank);
	}

	/**
	 * 批量删除银行信息
	 *
	 * @param ids 需要删除的银行信息主键
	 * @return 结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteFinanceBankByIds(Long[] ids) {
		for (Long id : ids) {
			GwjFinanceBank financeBank = new GwjFinanceBank();
			financeBank.setId(id);
			financeBank.setDelFlag(UserStatus.DELETED.getCode());
			financeBankMapper.updateFinanceBank(financeBank);
		}
		return 1;
	}

	/**
	 * 删除银行信息信息
	 *
	 * @param id 银行信息主键
	 * @return 结果
	 */
	@Override
	public int deleteFinanceBankById(Long id) {
		GwjFinanceBank financeBank = new GwjFinanceBank();
		financeBank.setId(id);
		financeBank.setDelFlag(UserStatus.DELETED.getCode());
		return financeBankMapper.updateFinanceBank(financeBank);
	}

	/**
	 * 通过银行ID和地区ID查询银行信息
	 * @param bankId 银行ID
	 * @param areaId 地区ID
	 * @return 结果
	 */
	@Override
	public GwjFinanceBank selectFinanceBankByBankIdAndAreaId(Long bankId, Long areaId) {
		return financeBankMapper.selectFinanceBankByBankIdAndAreaId(bankId, areaId);
	}
}
